----------------
-- Select last tracklog of device by devicekey AlarmCode = '121'
-- k-phu
----------------
CREATE PROCEDURE TrackLog_GetLast
@DEVICEKEY VARCHAR(100)
AS
	SELECT TOP 1 tl.*
		   ,DEVICEKEYSECURITY
	  FROM VVTRACKLOG tl
	  JOIN DEVICE dv
	    ON tl.DEVICEKEY = dv.DEVICEKEY
	  WHERE dv.DEVICEKEY = @DEVICEKEY
		AND tl.LATITUDE <> '0'
		AND tl.LATITUDE <> '-0'
		AND tl.ALARMCODE = '121'
	  ORDER BY TRACKID DESC
GO
----------------
-- Select last tracklog alarm of device by devicekey AlarmCode <> '121'
-- k-phu
----------------
CREATE PROCEDURE TrackLog_GetLastAlarm
@DEVICEKEY VARCHAR(100)
AS
SELECT TOP 1 
	   tl.*,
	   DeviceKeySecurity
  FROM vvTrackLog tl
  JOIN Device dv
    ON tl.DeviceKey = dv.DeviceKey
 WHERE dv.DeviceKey = @DeviceKey
   AND tl.Latitude <> '0'
   AND tl.Latitude <> '-0'
   AND tl.AlarmCode <> '121'
 ORDER BY TrackID DESC
--execute TrackLog_GetLast '45214920739'
--execute TrackLog_GetLastAlarm '45214920739'
